using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace atomtask
{
	[Activity (Label = "")]			
	public class LoginActivity : Activity
	{
		private string username;
		private string password;

		private ImageButton back;
		private Button signUp;
		private EditText editUserame;
		private EditText editPassword;
		private Button login;
		private Dialog dialogLoading;

		private void init()
		{
			back = FindViewById<ImageButton> (Resource.Id.btn_Login_Back);
			signUp = FindViewById<Button> (Resource.Id.btn_Login_signUp);
			editUserame = FindViewById<EditText> (Resource.Id.etx_Login_Username);
			editPassword = FindViewById<EditText> (Resource.Id.etx_Login_Password);
			login = FindViewById<Button> (Resource.Id.btn_Login_Login);

			// Click Back button
			back.Click += new EventHandler (click_back);

			// Click Sign Up button
			signUp.Click += new EventHandler (click_signUp);

			// Click Login button
			login.Click += new EventHandler (click_login);

			// Loading dialog initialization
			dialogLoading = new Dialog (this, Resource.Style.dialog_theme);
			dialogLoading.SetContentView (Resource.Layout.Loading);
		}

		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "Login" layout
			SetContentView (Resource.Layout.Login);

			// Initialize
			init ();
		}

		private void click_back (object sender, EventArgs arg)
		{
			disableAllWidget ();

			StartActivity (typeof(OnOffActivity));
			this.Finish ();
		}

		private void click_signUp (object sender, EventArgs arg)
		{
			disableAllWidget ();

			StartActivity (typeof(SignUpActivity));
			this.Finish ();
		}

		private void click_login (object sender, EventArgs arg)
		{
			disableAllWidget ();
			RunOnUiThread (() => {
				dialogLoading.Show ();
			});

			username = editUserame.Text;
			password = editPassword.Text;

			if (!GlobalData.User.checkUsername (username) || !GlobalData.User.checkPassword (password)) {
				RunOnUiThread (() => {
					Toast.MakeText (this, Resource.String.e_login_invalid_input, ToastLength.Short).Show ();
					enableAllWidget ();
					dialogLoading.Dismiss();
				});
				return;
			}

			// Send request to the servers
			password = GlobalData.User.GetMd5Hash (username) + GlobalData.User.GetMd5Hash (password);

			GlobalData.RequestSender requestsender = new GlobalData.RequestSender ("login");
			requestsender.addParameter ("username", username);
			requestsender.addParameter ("password", password);
			requestsender.addComplete (this.downloadComplete);
			requestsender.send ();
		}

		public void downloadComplete(object s, UploadValuesCompletedEventArgs e)
		{
			byte[]data = (byte[])e.Result;
			string permission;
			if (data == null) {
				RunOnUiThread (() => {
					Toast.MakeText(this, Resource.String.e_connect, ToastLength.Short).Show();
					enableAllWidget ();
					dialogLoading.Dismiss();
				});
				return;
			} else
				permission = Encoding.UTF8.GetString (data);

			// Handle the downloaded data as XML
			GlobalData.XMLHandler xml = new GlobalData.XMLHandler ();
			if (!xml.loadXML (permission)) {
				RunOnUiThread (() => {
					Toast.MakeText (this, Resource.String.e_unknown_error, ToastLength.Short).Show ();
					enableAllWidget ();
					dialogLoading.Dismiss();
				});
				return;
			}

			// Read the result
			string result = xml.readContent ();
			if (result == "s_login") {
				GlobalData.User.username = username;
				GlobalData.User.user_id = xml.readContent();
				GlobalData.User.token = xml.readContent();
				StartActivity (typeof(MainMenuActivity));
				this.Finish ();
			} else {
				RunOnUiThread (() => {
					Toast.MakeText(this, RenderString(result), ToastLength.Short).Show();
					enableAllWidget ();
					dialogLoading.Dismiss();
				});
			}
		}

		// Disable all widgets
		private void disableAllWidget()
		{
			back.Enabled = false;
			signUp.Enabled = false;
			editUserame.Enabled = false;
			editPassword.Enabled = false;
			login.Enabled = false;
		}

		// Enable all widgets
		private void enableAllWidget()
		{
			back.Enabled = true;
			signUp.Enabled = true;
			editUserame.Enabled = true;
			editPassword.Enabled = true;
			login.Enabled = true;
		}

		// Render string
		public string RenderString(string key)
		{
			return GlobalData.Resources.RenderString (key, this);
		}
	}
}

